﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using WebStickyNotes.Data.Access;
using WebStickyNotes.Helpers;
using WebStickyNotes.Models;

namespace WebStickyNotes.Controllers
{
	[HandleError]
    public class MobileController : Controller
    {
        private readonly string _connectionType;
        private readonly string _connectionString;

        private static readonly List<int> AllowedTaskRoleId = new List<int> { 1, 2 };

		public MobileController()
        {
            _connectionType = ConfigurationManager.AppSettings["ConnectionType"];
            _connectionString = ConfigurationManager.ConnectionStrings["WebStickyNotesData"].ConnectionString;
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult Index()
        {
            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                var model = new MobileIndexModel
                                {
                                    Projects = FilteredDataHelper.GetFilteredTaskProjectsForUser(supplier.DataAccess, currentUser)
                                };

                return View(model);
            }
        }

        [HttpGet]
        [MustSignIn]
		public ActionResult Streams(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileStreamsModel
				{
					Streams = supplier.DataAccess.GetStreamsByProject(id)
				};

			    model.ProjectName = supplier.DataAccess.GetTaskProject(id).Title;

				return View(model);
			}
		}

        [HttpGet]
        [MustSignIn]
		public ActionResult Tasks(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTasksModel
				{
					Tasks = supplier.DataAccess.GetTasksByStream(id)
				};

			    var taskStream = supplier.DataAccess.GetTaskStream(id);

			    model.ProjectId = taskStream.TaskProjectId;
			    model.StreamName = taskStream.Title;

				return View(model);
			}
		}

        [HttpGet]
        [MustSignIn]
		public ActionResult Task(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskModel
				{
					Task = supplier.DataAccess.GetTaskDetail(id),
					CurrentUser = currentUser
				};

				return View(model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangePerson(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskChangePersonModel
				            	{
				            		Task = supplier.DataAccess.GetTaskDetail(id),
				            		CurrentUser = currentUser,
				            		People = supplier.DataAccess.GetAllTaskPeople()
				            	};

				return View(model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskSaveChangePerson(int taskId, int personId)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.TaskPersonId = personId;
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				extendedTaskDetail.Person = supplier.DataAccess.GetTaskPerson(personId);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangeStream(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

			    var model = new MobileTaskChangeStreamModel
			                    {
			                        Task = supplier.DataAccess.GetTaskDetail(id),
			                        CurrentUser = currentUser
				};



			    int projectId = supplier.DataAccess.GetTaskStream(model.Task.Task.TaskStreamId).TaskProjectId;

                model.Streams = FilteredDataHelper.GetFilteredTaskStreamsForProjectAndUser(supplier.DataAccess, currentUser, projectId);

				return View(model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskSaveChangeStream(int taskId, int streamId)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.TaskStreamId = streamId;
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				extendedTaskDetail.Stream = supplier.DataAccess.GetTaskStream(streamId);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangePriority(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskChangePriorityModel
				{
					Task = supplier.DataAccess.GetTaskDetail(id),
					CurrentUser = currentUser,
					Priorities = supplier.DataAccess.GetAllTaskPriorities()
				};

				return View(model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskSaveChangePriority(int taskId, int priorityId)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.TaskPriorityId = priorityId;
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				extendedTaskDetail.Priority = supplier.DataAccess.GetTaskPriority(priorityId);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangeRelease(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskChangeReleaseModel
				{
					Task = supplier.DataAccess.GetTaskDetail(id),
					CurrentUser = currentUser
				};

				return View(model);
			}
		}

		[HttpPost]
        [MustSignIn]
		public ActionResult TaskSaveChangeRelease(int taskId, string release)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.Release = release;
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangeMilestone(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskChangeMilestoneModel
				{
					Task = supplier.DataAccess.GetTaskDetail(id),
					CurrentUser = currentUser
				};

				return View(model);
			}
		}

		[HttpPost]
        [MustSignIn]
		public ActionResult TaskSaveChangeMilestone(int taskId, string milestone)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.MilestoneDate = DateTime.Parse(milestone);
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskChangeProgress(int id)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var model = new MobileTaskChangeProgressModel
				{
					Task = supplier.DataAccess.GetTaskDetail(id),
					CurrentUser = currentUser
				};

				return View(model);
			}
		}

		[HttpGet]
        [MustSignIn]
		public ActionResult TaskSaveChangeProgress(int taskId, int progress)
		{
            using (var supplier = new Supplier(_connectionType, _connectionString))
			{
				var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
				if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
				{
					return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
				}

				var extendedTaskDetail = supplier.DataAccess.GetTaskDetail(taskId);
				extendedTaskDetail.Task.PercentageComplete = progress;
				supplier.DataAccess.SaveTaskDetail(extendedTaskDetail.Task);

				var model = new MobileTaskModel
				{
					Task = extendedTaskDetail,
					CurrentUser = currentUser
				};

				return View("Task", model);
			}
		}
    }
}